package net.NingXiaRuiTai.ShortChain.Manager.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import net.NingXiaRuiTai.ShortChain.Manager.ShortLinkManager;
import net.NingXiaRuiTai.ShortChain.controller.request.ShortLinkPageRequest;
import net.NingXiaRuiTai.ShortChain.interceptor.LoginInterceptor;
import net.NingXiaRuiTai.ShortChain.mapper.ShortLinkMapper;
import net.NingXiaRuiTai.ShortChain.model.ShortLinkDO;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Map;

@Component
public class ShortLinkManagerImpl implements ShortLinkManager {
    @Resource
    private ShortLinkMapper shortLinkMapper;

    @Override
    public int addShortLink(ShortLinkDO shortLinkDO) {


        return shortLinkMapper.insert(shortLinkDO);
    }

    @Override
    public ShortLinkDO findByShortLinkCode(String shortLinkCode) {
        ShortLinkDO shortLinkDO = shortLinkMapper.selectOne(new QueryWrapper<ShortLinkDO>().eq("code", shortLinkCode).eq("del", 0));
        return shortLinkDO;
    }

    @Override
    public int del(String shortLinkCode, Long accountNo) {

        ShortLinkDO shortLinkDO = new ShortLinkDO();
        shortLinkDO.setDel(1);
        int rows = shortLinkMapper.update(shortLinkDO, new QueryWrapper<ShortLinkDO>().eq("code", shortLinkCode).eq("account_no", accountNo
        ));
        return rows;
    }

    @Override
    public int delShortLink(ShortLinkDO shortLinkDO) {

        int rows = shortLinkMapper.update(null,
                new UpdateWrapper<ShortLinkDO>()
                        .eq("code", shortLinkDO.getCode())
                        .eq("account_no", shortLinkDO.getAccountNo())
                        .set("del",1));
        return rows;


    }

    @Override
    public int updateShortLink(ShortLinkDO shortLinkDO) {
        int rows = shortLinkMapper.update(null, new UpdateWrapper<ShortLinkDO>()
                .eq("code", shortLinkDO.getCode())
                .eq("del", 0)
                .eq("account_no",shortLinkDO.getAccountNo())

                .set("title", shortLinkDO.getTitle())
                .set("domain", shortLinkDO.getDomain()));


        return rows;


    }


}
